package com.dp.mallchat.common.user.dao;

import com.dp.mallchat.common.user.domain.user.entity.Black;
import com.dp.mallchat.common.user.mapper.BlackMapper;
import com.dp.mallchat.common.user.service.IBlackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 黑名单 服务实现类
 * </p>
 *
 * @author <a href="https://gitee.com/baiDP">adu</a>
 * @since 2024-06-13
 */
@Service
public class BlackDao extends ServiceImpl<BlackMapper, Black> {

    public Black getByUid(Long uid, Integer type) {
        return lambdaQuery()
                .eq(Black::getType, type)
                .eq(Black::getTarget, uid)
                .one();
    }

    @Cacheable(cacheNames = "black", key = "'blackList'", unless = "#result.size() == 0")
    public Map<Integer, Set<String>> blackList(){
        List<Black> list = list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.EMPTY_MAP;
        }
        Map<Integer, List<Black>> map = list.stream().collect(Collectors.groupingBy(Black::getType));
        Map<Integer, Set<String>> result = new HashMap<>();
        for (Map.Entry<Integer, List<Black>> entry : map.entrySet()) {
            result.put(entry.getKey(), entry.getValue().stream().map(Black::getTarget).collect(Collectors.toSet()));
        }
        return result;
    }

    @CacheEvict(cacheNames = "black", key = "'blackList'")  //Map<Integer, Set<String>>
    public List<Black> blackListEvict(){
       return null;
    }

}
